<?php
/*
Plugin Name: Nokia Places
Plugin URI: http://api.maps.nokia.com/places/
Description: With this plugin you are able to add a place into a post or a page.
Version: 0.1
Author: Nikolai Tihhomirov @ Ixonos 
Author Email: nikolai.tihhomirov@ixonos.com
License: 
*/

/**
 * add_placesapi_core
 * 
 * @package Nokia Places Plugin
 * Insert placesapi core Javascript files into wordpress except admin pages
 *
 */
if ( !is_admin() ){
wp_enqueue_script( 'placesapi', plugins_url( '/placesapi/jsPlacesAPI-en_GB.js', __FILE__ ) );
wp_enqueue_script( 'placesapi_config', plugins_url( '/placesapi/configuration.js', __FILE__ ), array( 'placesapi' ) );
wp_enqueue_style( 'custom_template', plugins_url( '/templates/custom/custom.css', __FILE__ ) );
} else {
/**
 * add_colorbox_core
 * 
 * @package Nokia Places Plugin
 * Insert colorbox core Javascript files into admin pages
 *
 */
wp_register_script( 'colorbox', plugins_url( '/tinymce/colorbox/jquery.colorbox.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_script( 'colorbox_hooks', plugins_url( '/tinymce/colorbox/hooks.js', __FILE__ ), array( 'jquery', 'colorbox' ) );
wp_enqueue_style( 'colorbox', plugins_url( '/tinymce/page/colorbox.css', __FILE__ ) );
}

/**
 * get_nokiaplaces_url
 * 
 * @package Nokia Places Plugin
 * Get plugin_url > used in tinymce.php
 *
 */
function nokiaplaces_url( $path = '' ) {
	global $wp_version;
	if ( version_compare( $wp_version, '2.8', '<' ) ) { // Using WordPress 2.7
		$folder = dirname( plugin_basename( __FILE__ ) );
		if ( '.' != $folder )
			$path = path_join( ltrim( $folder, '/' ), $path );

		return plugins_url( $path );
	}
	return plugins_url( $path, __FILE__ );
}
include_once ( dirname (__FILE__) . '/tinymce/tinymce.php' );


/**
 * allow_iframe
 * 
 * Allows iframe in editor
 *
 */
function add_iframe( $initArray ) {
$initArray['extended_valid_elements'] = "iframe[id|frameborder|height|scrolling|src|width]";
return $initArray;
}
add_filter( 'tiny_mce_before_init', 'add_iframe' );

/**
 *  
 * @package Nokia Places Plugin
 * Register Nokia places shortcode and the way extracting it
 *
 */
// [nokia template="template" place="placeId"]
function nokia_place_shortcode( $atts ) {
extract( shortcode_atts( array(
		'place' => 'abc',
		'template' => 'def',
	), $atts) );
	$insert_code = create_nokia_places_post( $place, $template );
	return $insert_code;
}
add_shortcode( 'nokia', 'nokia_place_shortcode' );

/**
 * insert_nokiaplace
 * 
 * @package Nokia Places Plugin
 * Insert nokia places basic place
 *
 */
function create_nokia_places_post( $id, $template ) {
if(isset( $id )and isset( $template )){
//Check templates
switch ( $template ){
	case "custom":
	$temp = "custom";
	//insert custom template div
	insert_custom_template();
	break;	
	case "default":
	default:
	$temp = "nokia.general.place";
	break;
}
	//make unique id for container
	$div_name = uniqid();
	//Replace shortcode with div+js core
	$code = "<div id='$div_name'></div>
	<script type='text/javascript'>
	var basicPlace = new nokia.places.Place({
         placeId: '$id',
         targetNode: '$div_name',
		 template: '$temp'});
        </script>";
   }else{
   //if id or template is missing
   	$code = "<p>Error parsing place: something is missing or wrong</p>";
   }  
   return $code;
}


/**
 * insert_custom_template_div
 * 
 * @package Nokia Places Plugin
 * 
 */
function insert_custom_template(){
include_once ( dirname ( __FILE__ ) . '/templates/custom/custom.html' );
}
?>